<template>
  <div class="knowledge-point">
    <!-- 知识点头部 -->
    <div class="point-header">
      <div class="point-info">
        <el-tag>{{ point.courseName }}</el-tag>
        <h1>{{ point.title }}</h1>
      </div>
      <div class="point-meta">
        <el-tag :type="point.difficultyType">{{ point.difficulty }}</el-tag>
        <span>掌握度: {{ point.masteryLevel }}%</span>
      </div>
    </div>

    <!-- 知识点内容区域 -->
    <el-row :gutter="20">
      <el-col :span="16">
        <!-- 知识点详细内容 -->
        <div class="content-section">
          <el-card>
            <template #header>
              <div class="content-header">
                <h2>知识点详解</h2>
                <el-button-group>
                  <el-button :type="viewMode === 'text' ? 'primary' : ''" @click="viewMode = 'text'">
                    文字模式
                  </el-button>
                  <el-button :type="viewMode === 'mind' ? 'primary' : ''" @click="viewMode = 'mind'">
                    思维导图
                  </el-button>
                </el-button-group>
              </div>
            </template>

            <!-- 文字模式 -->
            <div v-if="viewMode === 'text'" v-html="point.content" class="point-content"></div>
            
            <!-- 思维导图模式 -->
            <div v-else class="mind-map">
              <!-- 这里可以集成思维导图组件 -->
              <img :src="point.mindMapUrl" alt="思维导图" />
            </div>
          </el-card>

          <!-- 示例代码 -->
          <el-card v-if="point.codeExamples" class="code-examples">
            <template #header>
              <h3>示例代码</h3>
            </template>
            <div v-for="(example, index) in point.codeExamples" :key="index" class="code-example">
              <h4>{{ example.title }}</h4>
              <pre><code>{{ example.code }}</code></pre>
              <p>{{ example.explanation }}</p>
            </div>
          </el-card>

          <!-- 相关练习 -->
          <el-card class="related-exercises">
            <template #header>
              <div class="section-header">
                <h3>相关练习</h3>
                <el-button type="primary" text>查看更多</el-button>
              </div>
            </template>
            <div class="exercise-list">
              <el-card v-for="exercise in point.relatedExercises" 
                      :key="exercise.id"
                      class="exercise-item"
                      shadow="hover"
                      @click="navigateToExercise(exercise.id)">
                <div class="exercise-info">
                  <h4>{{ exercise.title }}</h4>
                  <div class="exercise-meta">
                    <el-tag size="small" :type="exercise.difficultyType">{{ exercise.difficulty }}</el-tag>
                    <span>通过率: {{ exercise.passRate }}%</span>
                  </div>
                </div>
              </el-card>
            </div>
          </el-card>
        </div>
      </el-col>

      <!-- 右侧学习路径 -->
      <el-col :span="8">
        <div class="learning-path">
          <el-card>
            <template #header>
              <h3>学习路径</h3>
            </template>
            <el-steps direction="vertical">
              <el-step 
                v-for="step in point.learningPath" 
                :key="step.id"
                :title="step.title"
                :description="step.description"
                :status="step.status"
              />
            </el-steps>
          </el-card>

          <!-- 相关知识点 -->
          <el-card class="related-points">
            <template #header>
              <h3>相关知识点</h3>
            </template>
            <div class="point-map">
              <!-- 前置知识点 -->
              <div class="prerequisites">
                <h4>前置知识点</h4>
                <el-tag 
                  v-for="pre in point.prerequisites"
                  :key="pre.id"
                  :type="pre.mastered ? 'success' : 'warning'"
                  class="point-tag"
                  @click="navigateToPoint(pre.id)"
                >
                  {{ pre.title }}
                </el-tag>
              </div>
              
              <!-- 后续知识点 -->
              <div class="next-points">
                <h4>后续知识点</h4>
                <el-tag 
                  v-for="next in point.nextPoints"
                  :key="next.id"
                  :type="next.unlocked ? 'info' : 'danger'"
                  class="point-tag"
                  @click="navigateToPoint(next.id)"
                >
                  {{ next.title }}
                </el-tag>
              </div>
            </div>
          </el-card>

          <!-- 学习建议 -->
          <el-card class="learning-suggestions">
            <template #header>
              <h3>学习建议</h3>
            </template>
            <ul class="suggestion-list">
              <li v-for="(suggestion, index) in point.suggestions" 
                  :key="index"
                  class="suggestion-item">
                <el-icon><InfoFilled /></el-icon>
                <span>{{ suggestion }}</span>
              </li>
            </ul>
          </el-card>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { InfoFilled } from '@element-plus/icons-vue'

const router = useRouter()
const viewMode = ref('text')

// 示例数据
const point = ref({
  courseName: '数据结构与算法',
  title: '快速排序',
  difficulty: '中等',
  difficultyType: 'warning',
  masteryLevel: 75,
  content: `
    <h3>快速排序算法介绍</h3>
    <p>快速排序是一种高效的排序算法，使用分治法（Divide and Conquer）策略来把一个序列分为较小和较大的2个子序列，然后递归地排序两个子序列。</p>
    <h4>算法步骤：</h4>
    <ol>
      <li>从数列中挑出一个元素，称为"基准"（pivot）</li>
      <li>重新排序数列，所有比基准值小的元素摆放在基准前面，所有比基准值大的元素摆在基准后面</li>
      <li>递归地把小于基准值元素的子数列和大于基准值元素的子数列排序</li>
    </ol>
  `,
  mindMapUrl: '/path/to/mindmap.png',
  codeExamples: [
    {
      title: '基本实现',
      code: `function quickSort(arr) {
  if (arr.length <= 1) return arr;
  const pivot = arr[arr.length - 1];
  const left = [], right = [];
  
  for (let i = 0; i < arr.length - 1; i++) {
    if (arr[i] < pivot) {
      left.push(arr[i]);
    } else {
      right.push(arr[i]);
    }
  }
  
  return [...quickSort(left), pivot, ...quickSort(right)];
}`,
      explanation: '这是一个简单的快速排序实现，使用了额外的空间来存储左右子数组。'
    }
  ],
  relatedExercises: [
    {
      id: 1,
      title: '实现快速排序的 partition 函数',
      difficulty: '中等',
      difficultyType: 'warning',
      passRate: 65
    },
    {
      id: 2,
      title: '优化快速排序的空间复杂度',
      difficulty: '困难',
      difficultyType: 'danger',
      passRate: 45
    }
  ],
  learningPath: [
    {
      id: 1,
      title: '理解分治思想',
      description: '已完成',
      status: 'success'
    },
    {
      id: 2,
      title: '掌握快速排序原理',
      description: '学习中',
      status: 'process'
    },
    {
      id: 3,
      title: '优化与应用',
      description: '待学习',
      status: 'wait'
    }
  ],
  prerequisites: [
    { id: 1, title: '分治算法', mastered: true },
    { id: 2, title: '递归', mastered: true }
  ],
  nextPoints: [
    { id: 3, title: '归并排序', unlocked: true },
    { id: 4, title: '堆排序', unlocked: false }
  ],
  suggestions: [
    '建议先复习递归和分治的基本概念',
    '可以通过手动模拟小规模数据来理解排序过程',
    '注意理解和处理边界情况',
    '建议完成相关练习来加深理解'
  ]
})

const navigateToExercise = (exerciseId) => {
  router.push(`/exercise/${exerciseId}`)
}

const navigateToPoint = (pointId) => {
  router.push(`/point/${pointId}`)
}
</script>

<style scoped>
.knowledge-point {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.point-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.point-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.point-meta {
  display: flex;
  align-items: center;
  gap: 16px;
}

.content-section > * {
  margin-bottom: 20px;
}

.content-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.point-content {
  line-height: 1.6;
}

.mind-map {
  text-align: center;
}

.mind-map img {
  max-width: 100%;
}

.code-example {
  margin-bottom: 20px;
}

.code-example pre {
  background: #f5f7fa;
  padding: 16px;
  border-radius: 4px;
  overflow-x: auto;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.exercise-item {
  margin-bottom: 12px;
  cursor: pointer;
  transition: transform 0.2s;
}

.exercise-item:hover {
  transform: translateY(-2px);
}

.exercise-meta {
  display: flex;
  gap: 12px;
  margin-top: 8px;
}

.point-map {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.prerequisites, .next-points {
  h4 {
    margin-bottom: 8px;
  }
}

.point-tag {
  margin: 4px;
  cursor: pointer;
}

.suggestion-list {
  list-style: none;
  padding: 0;
}

.suggestion-item {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 12px;
}

.learning-path > * {
  margin-bottom: 20px;
}
</style> 